package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.pojo.TbScheme;
import com.wang.pojo.TbSicknessScheme;
import com.wang.pojo.TbTask;
import com.wang.service.TbSchemeService;
import com.wang.service.TbSicknessSchemeService;
import com.wang.mapper.TbSicknessSchemeMapper;
import com.wang.service.TbTaskService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 *疾病方案任务链接
 */
@Service
public class TbSicknessSchemeServiceImpl extends ServiceImpl<TbSicknessSchemeMapper, TbSicknessScheme>
    implements TbSicknessSchemeService{

    @Resource
    private  TbSicknessSchemeMapper tbSicknessSchemeMapper;

    @Resource
    private TbSchemeService tbSchemeService; //方案的

    @Resource
    private TbTaskService tbTaskService; //任务的


    /*
     * 根据的方案sid获取中间表的list
     * */

    public List<TbTask> gettbtask(Integer sid){
        //方案的
        QueryWrapper<TbSicknessScheme> qw=new QueryWrapper<>();
        qw.eq("task_id", sid);
        List<TbSicknessScheme> tbSicknessSchemes = tbSicknessSchemeMapper.selectList(qw);

        return  null;
    }



    @Override
    public HashSet<TbScheme> getschemestasklist(Integer sid) {
        //sid 疾病id
       // ArrayList<TbScheme> list = new ArrayList<>();  //方案的
        HashSet<TbScheme> list = new HashSet<>();
        QueryWrapper<TbSicknessScheme> qw=new QueryWrapper<>();
        qw.eq("sid", sid);
        List<TbSicknessScheme> sicknessSchemeList = this.tbSicknessSchemeMapper.selectList(qw);

        for (TbSicknessScheme tbSicknessScheme : sicknessSchemeList) {
            TbScheme tbScheme= tbSchemeService.getschemebysid(tbSicknessScheme.getFid());
            //方案这个
            List<TbSicknessScheme> list1=this.gettaskidlistbysidfid(sid,tbScheme.getSid());
            //方案疾病对应的任务
            ArrayList<TbTask> list11 = new ArrayList<>();  //任务的
            for (TbSicknessScheme sicknessScheme : list1) {
                TbTask gettbtask = this.tbTaskService.gettbtask(sicknessScheme.getTaskId());
                list11.add(gettbtask);
            }
            tbScheme.setTaskList(list11);
            list.add(tbScheme);
        }
        return list;
    }

    @Override
    public void addnewsicksche(TbSicknessScheme tbSicknessScheme) {
        this.tbSicknessSchemeMapper.insert(tbSicknessScheme);
    }


    private List<TbSicknessScheme> gettaskidlistbysidfid(Integer sid, Integer sid1) {
        return this.tbSicknessSchemeMapper.gettaskidlistbysidfid(sid,sid1);
    }
}




